<?php

namespace app\admin\controller;
use app\common\controller\Admin;
class Adsmaterialcategory extends Admin {
    /**
     * 小说对外开放接口
     *
     * @Author 小邓
     * @DateTime 2022-11-03
     * @return void
     */
	public function _initialize() {
		parent::_initialize();
        $types = [
            [ "id" => 'wenan', "name" => "文案"],
            [ "id" => 'img', "name" => "图片"],
            [ "id" => 'video', "name" => "视频"],
            [ "id" => 'bgm', "name" => "bgm"],
        ];
        $this->assign(compact('types'));
    }

    public function index(){
        $keyword = input('get.keyword');
        $map     = [];
        if($keyword){
            $map['id|title'] = ["like","%$keyword%"];
        }
        $list = db('ads_material_category')->where($map)->paginate( 200, false, [ 'type'=> 'page\Page' ,'query' => request()->param()] );
        foreach ($list as $k => $v) {
            $list[$k] = $v;
        }
        $page = $list->render();
        $this->assign(compact('keyword', 'list', 'page'));
        $this->setMeta("账号管理");
        return view();
    }

    public function add() {
        if(IS_POST){
            $post = input('post.');
            if( !$post['id'] ){
                // $post['apikey'] = md5(uniqid());
                db('ads_material_category')->insert($post);
            }else{
                db('ads_material_category')->update($post);
            }
            return $this->success("添加成功！");
        }
        return $this->error("失败！");
    }

    public function del() {
		$id = $this->getArrayParam('id');
		if (empty($id)) {
			return $this->error("非法操作！");
		}
		$map['id'] = array('IN', $id);
		$result    = db('ads_material_category')->where($map)->delete();
		if (false !== $result) {
			return $this->success("删除成功！");
		} else {
			return $this->error("删除失败！");
		}
	}


}